package store.lunangangster.engine.impl.agenda;

import store.lunangangster.bpmn.model.FlowElement;
import store.lunangangster.bpmn.model.FlowNode;
import store.lunangangster.engine.NodeFlowException;
import store.lunangangster.engine.impl.bpmn.behavior.TriggerableModelBehavior;
import store.lunangangster.engine.impl.entity.ExecutionEntity;
import store.lunangangster.engine.impl.interceptor.CommandContext;

public class TriggerExecutionOperation extends AbstractOperation {

  public TriggerExecutionOperation(ExecutionEntity execution,
      CommandContext commandContext) {
    super(execution, commandContext);
  }

  @Override
  public void run() {
    FlowElement currentFlowElement = execution.getCurrentFlowElement();

    if (currentFlowElement instanceof FlowNode) {
      Object behavior = ((FlowNode) currentFlowElement).getBehavior();
      if (behavior instanceof TriggerableModelBehavior) {
        ((TriggerableModelBehavior) behavior).trigger(execution);
      } else {
        throw new NodeFlowException("元素" + currentFlowElement.getClass() + "的ModelBehavior应实现TriggerableModelBehavior接口");
      }
    } else {
      throw new NodeFlowException("非FlowNode元素无法触发TriggerExecutionOperation");
    }
  }
}
